package cn.xpride.framework.application;

import cn.xpride.framework.http.SmartHttpContext;
import cn.xpride.framework.http.annotation.GetMapping;
import cn.xpride.framework.http.annotation.PostMapping;
import cn.xpride.framework.http.annotation.RestController;
import cn.xpride.framework.task.TaskRunnerContext;
import cn.xpride.framework.utils.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

@Slf4j
@RestController
public class DocRestController {
    
    @GetMapping("swagger-resources")
    public JSONArray sr() {
        File srFile = new File(SmartHttpContext.getClassDir(),"json/swagger-resources");
        log.info("请求swagger-resources文件:{}",srFile.getAbsolutePath());
        if (srFile.exists()){
            return JSONArray.parseArray(FileUtil.readFileString(srFile.getAbsolutePath()));
        }else {
            return new JSONArray();
        }
    }

    @GetMapping("**/api-docs")
    public JSONObject apiDoc() {
        String uri = SmartHttpContext.currentHttpRequest().getRequestURI();
        File jsonFile = new File(SmartHttpContext.getClassDir(),"json"+uri);
        if(jsonFile.exists()){
            return JSONObject.parseObject(FileUtil.readFileString(jsonFile.getAbsolutePath()));
        }else {
            return new JSONObject();
        }
    }

    @PostMapping("startJob")
    public void startJob() {
        TaskRunnerContext.start();
    }

    @PostMapping("stopJob")
    public void stopJob() {
        TaskRunnerContext.stop();
    }

    @PostMapping("runOnce")
    public void runOnce() {
        TaskRunnerContext.runOnce();
    }

}
